package edu.msg.jthr.backend.bean;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.msg.jthr.backend.exception.ServiceException;
import edu.msg.jthr.backend.model.Department;
import edu.msg.jthr.backend.repository.exception.RepositoryException;
import edu.msg.jthr.backend.repository.service.DepartmentRepository;
import edu.msg.jthr.backend.service.DepartmentService;

@Stateless (name = "DepartmentServiceBean", mappedName = "ejb/DepartmentService")
public class DepartmentServiceBean implements DepartmentService {

	@EJB
	private DepartmentRepository departmentRepository;
	private static Logger LOG = LoggerFactory
			.getLogger(CandidateServiceBean.class);

	@Override
	public List<Department> getAll() throws ServiceException {
		try {
			return departmentRepository.getAll();
		} catch (RepositoryException e) {
			LOG.error("Get all department error!" + e.getMessage());
			throw new ServiceException("Department getById error!", e);
		}
	}

	@Override
	public Department getById(Long id) throws ServiceException {
		try {
			return departmentRepository.getById(id);
		} catch (RepositoryException e) {
			LOG.error("Department getById error!" + e.getMessage());
			throw new ServiceException("Department getById error!", e);
		}
	}

	@Override
	public void create(Department obj) throws ServiceException {
		try {
			departmentRepository.create(obj);
		} catch (RepositoryException e) {
			LOG.error("Department creating failure!" + e.getMessage());
			throw new ServiceException("Department creating failure!",
					e);
		}
	}

	@Override
	public void delete(Department obj) throws ServiceException {
		try {
			departmentRepository.delete(obj);
		} catch (RepositoryException e) {
			LOG.error("Department delete failure" + e.getMessage());
			throw new ServiceException("Department delete failure!", e);
		}
	}

	@Override
	public Department update(Department obj) throws ServiceException {
		try {
			return departmentRepository.update(obj);
		} catch (RepositoryException e) {
			LOG.error("Department update failure!" + e.getMessage());
			throw new ServiceException("Department update failure!", e);
		}
	}

}
